package com.attendance.service;

import com.alibaba.fastjson.JSONObject;
import com.attendance.annotation.Supplement;
import com.attendance.annotation.type.OperationType;
import com.attendance.java.DateUtils;
import com.attendance.java.StringUtils;
import com.attendance.mapper.DepartmentMapper;
import com.attendance.mapper.TaskDepartMapper;
import com.attendance.mapper.TaskMapper;
import com.attendance.mapper.TaskUserLogMapper;
import com.attendance.pojo.Menu;
import com.attendance.pojo.Task;
import com.attendance.pojo.TaskDepart;
import com.attendance.pojo.TaskUserLog;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Time;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 任务服务
 *
 * @author 今朝
 * @date 2021/03/23
 */
@Service
public class TaskServiceImpl {
    @Autowired
    TaskMapper taskMapper;
    @Autowired
    TaskDepartMapper taskDepartMapper;
    @Autowired
    DepartmentMapper departmentMapper;

    public Page<Task> getListPage(Integer pageNum, Integer limit) {
        Page<Task> page = new Page<>(pageNum, limit);
        QueryWrapper<Task> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("idx");
        taskMapper.selectPage(page, queryWrapper);
        return page;
    }

    public Page<Task> getListPage(Integer pageNum, Integer limit, String searchParams) {
        JSONObject parse = JSONObject.parseObject(searchParams);
        QueryWrapper<Task> queryWrapper = new QueryWrapper<>();
        for (String key : parse.keySet()) {
            queryWrapper.like(key, parse.get(key));
        }
        queryWrapper.orderByAsc("idx");
        Page<Task> page = new Page<>(pageNum, limit);
        taskMapper.selectPage(page, queryWrapper);
        return page;
    }

    public Page<Task> getSelectPage(Integer pageNum, Integer limit, List<Long> taskIds) {
        Page<Task> page = new Page<>(pageNum, limit);
        QueryWrapper<Task> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",taskIds);
        queryWrapper.orderByAsc("idx");
        taskMapper.selectPage(page, queryWrapper);
        return page;
    }

    public Task selectById(Long id) {
        return taskMapper.selectById(id);
    }

    @Supplement(type = OperationType.UPDATE)
    public Boolean updateById(Task task) {
        return taskMapper.updateById(task) != 0;
    }

    @Supplement(type = OperationType.PUT)
    public Boolean add(Task task, String departIds) {
        List<Long> dIds = StringUtils.strToIdsByComma(departIds);
        if (taskMapper.insert(task) != 0) {
            for (Long id : dIds) {
                taskDepartMapper.insert(new TaskDepart(task.getId(), id));
            }
            return true;
        }
        return false;
    }

    @Supplement(type = OperationType.UPDATE)
    public Boolean updateById(Task task, String departIds) {
        QueryWrapper<TaskDepart> queryWrapper = new QueryWrapper();
        queryWrapper.eq("task_id", task.getId());
        taskDepartMapper.delete(queryWrapper);
        for (Long id : StringUtils.strToIdsByComma(departIds)) {
            taskDepartMapper.insert(new TaskDepart(task.getId(), id));
        }
        return taskMapper.updateById(task) != 0;
    }

    public List<Long> getDepartIdsById(Long id){
        return taskMapper.findDepartIdsById(id);
    }

    public Boolean delete(List<Integer> ids) {
        return ids.size() == taskMapper.deleteBatchIds(ids);
    }

    public List<Long> getTaskIdsById(Long id){
        return taskMapper.findTaskIdsById(id);
    }

    public List<String> getTaskTitleByIds(List<Long> ids) {
        QueryWrapper<Task> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("idx");
        queryWrapper.in("id", ids);
        List<Task> menus = taskMapper.selectList(queryWrapper);
        return menus.stream().map(Task :: getName).collect(Collectors.toList());
    }

}
